//
//  BaseNavBar.m
//  Learn
//
//  Created by apple on 2024/3/31.
//  Copyright © 2024 迟建国. All rights reserved.
//

#import "BaseNavBar.h"

@implementation BaseNavBar

- (instancetype)init
{
    self = [super init];
    if (self) {
        /** 页面*/
        [self initContentView];
    }
    return self ;
}
#pragma mark --- 初始化
- (void)initContentView
{
    CGRect rect = CGRectMake(0, 0, SCREEN_W, NavigationBarHeight);
    self.frame = rect ;
    self.userInteractionEnabled = YES;
    self.multipleTouchEnabled = YES ;
    self.backgroundColor  = [UIColor whiteColor];
    
    /** 导航标题初始化*/
    [self addSubview:self.navTitle];
    /** 左侧返回键图片*/
    [self addSubview:self.leftImage];
    /** 左侧返回键点击button，目的是增加点击范围*/
    [self addSubview:self.leftTouchBack];

}
- (void)setBgColor:(UIColor *)bgColor {
    self.backgroundColor = bgColor;
}

#pragma mark --- 设置导航标题
- (void)setTitle:(NSString *)title
{
    if (!IsStrEmpty(title)) {
        self.navTitle.text = title ;
        NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:self.navTitle.text attributes:@{NSFontAttributeName: [UIFont fontWithName:@"PingFang SC" size: 18],NSForegroundColorAttributeName: [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1.0]}];
        self.navTitle.attributedText = string;
    }
}

#pragma mark --- 隐藏左侧点击和对应的图片
- (void)hideLeftBackButton:(BOOL)is
{
    _leftImage.hidden = is;
    self.leftTouchBack.hidden = is ;
}
#pragma mark --- 设置导航标题颜色
- (void)setTitleColor:(UIColor *)titleColor
{
    self.navTitle.textColor = titleColor ;
}
- (UILabel *)navTitle
{
    if (!_navTitle) {
        _navTitle = [[UILabel alloc] initWithFrame:CGRectMake(40,SafeAreaTopHeight, SCREEN_W - 40 * 2, 44)];
        _navTitle.textColor = UIColor.blackColor;
        _navTitle.font = FONT_BODY_16;
        _navTitle.textAlignment = NSTextAlignmentCenter;
        _navTitle.userInteractionEnabled = YES;
        _navTitle.multipleTouchEnabled = YES ;
        _navTitle.font = [UIFont fontWithName:@"PingFang SC" size: 18];
    }
    return _navTitle ;
}
-(UIImageView *)leftImage
{
    if (!_leftImage) {
        _leftImage = [[UIImageView alloc]initWithFrame: CGRectMake(17, SafeAreaTopHeight, 9, 17)];
        _leftImage.centerY = self.leftTouchBack.centerY;
        _leftImage.userInteractionEnabled = YES;
        _leftImage.multipleTouchEnabled = YES ;
        _leftImage.image = [UIImage imageNamed:@"icon_back"];
    }
    return _leftImage ;
}
-(UIButton *)leftTouchBack
{
    if (!_leftTouchBack) {
        _leftTouchBack = [[UIButton alloc]initWithFrame:CGRectMake(0, SafeAreaTopHeight, 60, 44)];
        
        [_leftTouchBack addTarget:self action:@selector(leftClicked:) forControlEvents:UIControlEventTouchUpInside];
//        _leftTouchBack.layer.borderColor = UIColor.redColor.CGColor;
//        _leftTouchBack.layer.borderWidth = 1;
    }
    return _leftTouchBack ;
}
- (void)leftClicked:(UIButton *)button {
    if (self.leftClickedBlock) {
        self.leftClickedBlock();
    }
}
@end
